import {getYMD,getNowFormatDate,getDay,formatGMTDate,sortBy} from '../../../../assets/js/util.js' 

import echarts from 'echarts'

export default{
  data() {
    return{
      // 默认参数 和 URL
      SRC: '',
      DP: {
        p: '',
        f: '',
        uid: ''
      },
      prop: '',
      order: '',      
      loading: true,
      searchable: true,
      title:'广告日数据（渠道统计）',
      xlsxTitle:'',
      pickerOptions: {
        shortcuts: [{
          text: '近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '近15天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      ftitle:'',
      activeIndex1: "1",
      activeIndex2: "1",
      ruleForm :{
        sday:'',
        eday:'',
        startDate: '',
        attr:'',
        platForm: '',
        agent: '',
        settlWay: '',
        channel: '',
        adPlace:''
      },
      tableHead1 :[{"prop":"dateStr","label":"日期",fixed:true},{"prop":"channelName","label":"渠道名称",fixed:true},{"prop":"agent","label":"代理商",fixed:false},{"prop":"clkEquipment","label":"点击设备",fixed:false},{"prop":"actEquipment","label":"激活设备",fixed:false},{"prop":"newActive","label":"实际激活",fixed:false},{"prop":"newEquipment","label":"新增设备",fixed:false},{"prop":"newAccount","label":"新增账号",fixed:false},{"prop":"newPayUser","label":"新增付费账号",fixed:false},{"prop":"newPay","label":"新增付费金额",fixed:false},{"prop":"payrate","label":"付费率",fixed:false},{"prop":"discount","label":"折后成本",fixed:false},{"prop":"cpa","label":"CPA",fixed:false},{"prop":"totalpay","label":"累计充值【90日】",fixed:false},{"prop":"roi","label":"渠道后ROI(%)",fixed:false}],
      tableHead2 :[{"prop":"channel","label":"渠道"},{"prop":"clkEquipment","label":"点击设备"},{"prop":"actEquipment","label":"激活设备"},{"prop":"newActive","label":"实际激活"},{"prop":"newEquipment","label":"新增设备"},{"prop":"newAccount","label":"新增账号"},{"prop":"newPayUser","label":"新增付费账号"},{"prop":"newPay","label":"新增付费金额"},{"prop":"payrate","label":"付费率"},{"prop":"ltv","label":"ltv"},{"prop":"discount","label":"折后成本"},{"prop":"cpa","label":"CPA"},{"prop":"totalpay","label":"累计充值【90日】"},{"prop":"roi","label":"渠道后ROI(%)"}],
      tabHdD1S:['activeTranDatas','newRemainDatas','totalPayDatas'],
      tabHdD2S:['channelDetailDatas','activityDetailDatas','platformDetailDatas','agentDetailDatas','paytypeDetailDatas'],
      
      tableDataAll1:[],
      tableData1p:[],
      tableData1: [],
      tableDataAll2:[],
      tableData2P:[],
      tableData2: [],
      tableData3: [],
      /*当前页显示数量*/
      pageSize1: 10,
      pageSize2: 10,
      /*每页显示的数量*/
      pageSizes1: [10, 20, 50, 100],
      pageSizes2: [10, 20, 50, 100],
      /*dataTotal表数量*/
      dataTotal1: 0,
      dataTotal2:0,
      /*分页器*/
      currentPage2: 1,
      currentPage1: 1,
      // 归属
      attrs:[],
      // 渠道名称
      channels:[],
      // 代理商
      agents:[],
      // 平台
      platForms:[],
      // 结算方式
      settlWays:[],
      // 广告位置
      adPlaces:[],
      tableIndex:'',
      sheet:''
    }
  }, 
  computed:{
    
  },
  methods: {
    // 通用 成功 与 失败
    successFun(data) {
      this.$message({
        message: data,
        type: 'success'
      });
    },
    errorFun(data) {
      this.$message.error(data);
    },
    dateChange(val){
      if(val){
        var val = val.split(" ");
        this.sday = val[0];
        this.eday = val[2];
      }
    },
    agentChange(){

    },
    attrChange(){

    },
    channelChange(){

    },
    settlWayChange(){

    },
    adPlaceChange(){

    },
    loads() {
      this.loading = !this.loading; //加载中
      this.searchable = !this.searchable; //查询按钮不可用
    },
    search(){
      this.loads();
      this.apiAjax(this.sday,this.eday)
    },
    handleSizeChange1(val) {
      this.pageSize1 = val;
      this.tableData1p = sortBy(this.tableData1p,this.prop,this.order);
      this.dataTotal1 = this.tableData1p.length;
      this.tableData1 = this.tableData1p.slice((this.currentPage1-1)*(this.pageSize1),this.currentPage1*this.pageSize1);
    },
    handleCurrentChange1(val) {
      this.currentPage1 = val;
      this.tableData1p = sortBy(this.tableData1p,this.prop,this.order);
      this.dataTotal1 = this.tableData1p.length;
      this.tableData1 = this.tableData1p.slice((this.currentPage1-1)*(this.pageSize1),this.currentPage1*this.pageSize1);
    },
    handleSizeChange2(val) {
      this.pageSize2 = val;
      this.tableData2p = sortBy(this.tableData2p,this.prop,this.order);
      this.dataTotal2 = this.tableData2p.length;
      this.tableData2 = this.tableData2p.slice((this.currentPage2-1)*(this.pageSize2),this.currentPage2*this.pageSize2);
    },
    handleCurrentChange2(val) {
      this.currentPage2 = val;
      this.tableData2p = sortBy(this.tableData2p,this.prop,this.order);
      this.dataTotal2 = this.tableData2p.length;
      this.tableData2 = this.tableData2p.slice((this.currentPage2-1)*(this.pageSize2),this.currentPage2*this.pageSize2);
    },     
    edit (){

    },
    handleDelete (){

    },
    tabHdD1(o){
      if(o == 1){
        this.tableHead1 = [{"prop":"dateStr","label":"日期"},{"prop":"channelName","label":"渠道名称"},{"prop":"agent","label":"代理商"},{"prop":"clkEquipment","label":"点击设备"},{"prop":"actEquipment","label":"激活设备"},{"prop":"newActive","label":"实际激活"},{"prop":"newEquipment","label":"新增设备"},{"prop":"newAccount","label":"新增账号"},{"prop":"newPayUser","label":"新增付费账号"},{"prop":"newPay","label":"新增付费金额"},{"prop":"payrate","label":"付费率"},{"prop":"discount","label":"折后成本"},{"prop":"cpa","label":"CPA"},{"prop":"totalpay","label":"累计充值【90日】"},{"prop":"roi","label":"渠道后ROI(%)"}];
        this.activeIndex1 = '1';
        if(this.tableDataAll2['channelDetailDatas'].length>0){
            document.querySelector("#scrollAW .el-table__footer-wrapper").style.display="block";
       
        }else{
            document.querySelector("#scrollAW .el-table__footer-wrapper").style.display="none";
        }
        document.getElementById("aa1").scrollLeft = 0;
        this.sheet = "激活转化";
      }else if(o == 2){
        this.tableHead1 = [{"prop":"dateStr","label":"日期"},{"prop":"channelName","label":"渠道名称"},{"prop":"agent","label":"代理商"},{"prop":"newAccount","label":"新增账号"},{"prop":"remain2","label":"次日留存"},{"prop":"remain3","label":"3日留存"},{"prop":"remain4","label":"4日留存"},{"prop":"remain5","label":"5日留存"},{"prop":"remain6","label":"6日留存"},{"prop":"remain7","label":"7日留存"},{"prop":"remain8","label":"8日留存"},{"prop":"remain9","label":"9日留存"},{"prop":"remain10","label":"10日留存"},{"prop":"remain11","label":"11日留存"},{"prop":"remain12","label":"12日留存"},{"prop":"remain13","label":"13日留存"},{"prop":"remain14","label":"14日留存"},{"prop":"remain15","label":"15日留存"},{"prop":"remain16","label":"16日留存"},{"prop":"remain17","label":"17日留存"},{"prop":"remain18","label":"18日留存"},{"prop":"remain19","label":"19日留存"},{"prop":"remain20","label":"20日留存"},{"prop":"remain21","label":"21日留存"},{"prop":"remain22","label":"22日留存"},{"prop":"remain23","label":"23日留存"},{"prop":"remain24","label":"24日留存"},{"prop":"remain25","label":"25日留存"},{"prop":"remain26","label":"26日留存"},{"prop":"remain27","label":"27日留存"},{"prop":"remain28","label":"28日留存"},{"prop":"remain29","label":"29日留存"},{"prop":"remain30","label":"30日留存"},{"prop":"remain31","label":"31日留存"},{"prop":"remain32","label":"32日留存"},{"prop":"remain33","label":"33日留存"},{"prop":"remain34","label":"34日留存"},{"prop":"remain35","label":"35日留存"},{"prop":"remain36","label":"36日留存"},{"prop":"remain37","label":"37日留存"},{"prop":"remain38","label":"38日留存"},{"prop":"remain39","label":"39日留存"},{"prop":"remain40","label":"40日留存"},{"prop":"remain41","label":"41日留存"},{"prop":"remain42","label":"42日留存"},{"prop":"remain43","label":"43日留存"},{"prop":"remain44","label":"44日留存"},{"prop":"remain45","label":"45日留存"},{"prop":"remain46","label":"46日留存"},{"prop":"remain47","label":"47日留存"},{"prop":"remain48","label":"48日留存"},{"prop":"remain49","label":"49日留存"},{"prop":"remain50","label":"50日留存"},{"prop":"remain51","label":"51日留存"},{"prop":"remain52","label":"52日留存"},{"prop":"remain53","label":"53日留存"},{"prop":"remain54","label":"54日留存"},{"prop":"remain55","label":"55日留存"},{"prop":"remain56","label":"56日留存"},{"prop":"remain57","label":"57日留存"},{"prop":"remain58","label":"58日留存"},{"prop":"remain59","label":"59日留存"},{"prop":"remain60","label":"60日留存"},{"prop":"remain61","label":"61日留存"},{"prop":"remain62","label":"62日留存"},{"prop":"remain63","label":"63日留存"},{"prop":"remain64","label":"64日留存"},{"prop":"remain65","label":"65日留存"},{"prop":"remain66","label":"66日留存"},{"prop":"remain67","label":"67日留存"},{"prop":"remain68","label":"68日留存"},{"prop":"remain69","label":"69日留存"},{"prop":"remain70","label":"70日留存"},{"prop":"remain71","label":"71日留存"},{"prop":"remain72","label":"72日留存"},{"prop":"remain73","label":"73日留存"},{"prop":"remain74","label":"74日留存"},{"prop":"remain75","label":"75日留存"},{"prop":"remain76","label":"76日留存"},{"prop":"remain77","label":"77日留存"},{"prop":"remain78","label":"78日留存"},{"prop":"remain79","label":"79日留存"},{"prop":"remain80","label":"80日留存"},{"prop":"remain81","label":"81日留存"},{"prop":"remain82","label":"82日留存"},{"prop":"remain83","label":"83日留存"},{"prop":"remain84","label":"84日留存"},{"prop":"remain85","label":"85日留存"},{"prop":"remain86","label":"86日留存"},{"prop":"remain87","label":"87日留存"},{"prop":"remain88","label":"88日留存"},{"prop":"remain89","label":"89日留存"},{"prop":"remain90","label":"90日留存"}];
        this.activeIndex1 = '2';
        document.querySelector("#scrollAW .el-table__footer-wrapper").style.display="none";
        document.getElementById("aa1").scrollLeft = 0;
        this.sheet = "新增留存";
      }else if(o == 3){
        this.tableHead1 = [{"prop":"0","label":"日期"},{"prop":"1","label":"渠道名称"},{"prop":"2","label":"代理商"},{"prop":"3","label":"新增账号"},{"prop":"4","label":"1日收入"},{"prop":"5","label":"2日收入"},{"prop":"6","label":"3日收入"},{"prop":"7","label":"4日收入"},{"prop":"8","label":"5日收入"},{"prop":"9","label":"6日收入"},{"prop":"10","label":"7日收入"},{"prop":"11","label":"8日收入"},{"prop":"12","label":"9日收入"},{"prop":"13","label":"10日收入"},{"prop":"14","label":"11日收入"},{"prop":"15","label":"12日收入"},{"prop":"16","label":"13日收入"},{"prop":"17","label":"14日收入"},{"prop":"18","label":"15日收入"},{"prop":"19","label":"16日收入"},{"prop":"20","label":"17日收入"},{"prop":"21","label":"18日收入"},{"prop":"22","label":"19日收入"},{"prop":"23","label":"20日收入"},{"prop":"24","label":"21日收入"},{"prop":"25","label":"22日收入"},{"prop":"26","label":"23日收入"},{"prop":"27","label":"24日收入"},{"prop":"28","label":"25日收入"},{"prop":"29","label":"26日收入"},{"prop":"30","label":"27日收入"},{"prop":"31","label":"28日收入"},{"prop":"32","label":"29日收入"},{"prop":"33","label":"30日收入"},{"prop":"34","label":"31日收入"},{"prop":"35","label":"32日收入"},{"prop":"36","label":"33日收入"},{"prop":"37","label":"34日收入"},{"prop":"38","label":"35日收入"},{"prop":"39","label":"36日收入"},{"prop":"40","label":"37日收入"},{"prop":"41","label":"38日收入"},{"prop":"42","label":"39日收入"},{"prop":"43","label":"40日收入"},{"prop":"44","label":"41日收入"},{"prop":"45","label":"42日收入"},{"prop":"46","label":"43日收入"},{"prop":"47","label":"44日收入"},{"prop":"48","label":"45日收入"},{"prop":"49","label":"46日收入"},{"prop":"50","label":"47日收入"},{"prop":"51","label":"48日收入"},{"prop":"52","label":"49日收入"},{"prop":"53","label":"50日收入"},{"prop":"54","label":"51日收入"},{"prop":"55","label":"52日收入"},{"prop":"56","label":"53日收入"},{"prop":"57","label":"54日收入"},{"prop":"58","label":"55日收入"},{"prop":"59","label":"56日收入"},{"prop":"60","label":"57日收入"},{"prop":"61","label":"58日收入"},{"prop":"62","label":"59日收入"},{"prop":"63","label":"60日收入"},{"prop":"64","label":"61日收入"},{"prop":"65","label":"62日收入"},{"prop":"66","label":"63日收入"},{"prop":"67","label":"64日收入"},{"prop":"68","label":"65日收入"},{"prop":"69","label":"66日收入"},{"prop":"70","label":"67日收入"},{"prop":"71","label":"68日收入"},{"prop":"72","label":"69日收入"},{"prop":"73","label":"70日收入"},{"prop":"74","label":"71日收入"},{"prop":"75","label":"72日收入"},{"prop":"76","label":"73日收入"},{"prop":"77","label":"74日收入"},{"prop":"78","label":"75日收入"},{"prop":"79","label":"76日收入"},{"prop":"80","label":"77日收入"},{"prop":"81","label":"78日收入"},{"prop":"82","label":"79日收入"},{"prop":"83","label":"80日收入"},{"prop":"84","label":"81日收入"},{"prop":"85","label":"82日收入"},{"prop":"86","label":"83日收入"},{"prop":"87","label":"84日收入"},{"prop":"88","label":"85日收入"},{"prop":"89","label":"86日收入"},{"prop":"90","label":"87日收入"},{"prop":"91","label":"88日收入"},{"prop":"92","label":"89日收入"},{"prop":"93","label":"90日收入"}];
        this.activeIndex1 = '3';
        document.querySelector("#scrollAW .el-table__footer-wrapper").style.display="none";
        document.getElementById("aa1").scrollLeft = 0;
        this.sheet = "累计付费";
      }
      this.tableData1p = this.tableDataAll1[''+this.tabHdD1S[o-1]+''];
      this.dataTotal1 = this.tableData1p.length;
      this.currentPage1 = 1;
      this.tableData1 = this.tableData1p.slice((this.currentPage1-1)*(this.pageSize1),this.currentPage1*this.pageSize1);
      setTimeout(function(){
        document.querySelector("#scrollAW").style.width = document.querySelector("#scrollAW .el-table__header").style.width
      },200)
          
    },
    tabHdD2(o){
      if(o==1){
        this.tableHead2[0].prop = "channel";
        this.tableHead2[0].label = "渠道";
        this.activeIndex2 = '1';
        this.sheet = "渠道";
      }else if(o==2){
        this.tableHead2[0].prop = "activity";
        this.tableHead2[0].label = "活动";
        this.activeIndex2 = '2';
        this.sheet = "活动";
      }else if(o==3){
        this.tableHead2[0].prop = "platform";
        this.tableHead2[0].label = "平台";
        this.activeIndex2 = '3';
        this.sheet = "平台";
      }else if(o==4){
        this.tableHead2[0].prop = "agent";
        this.tableHead2[0].label = "代理";
        this.activeIndex2 = '4';
        this.sheet = "代理";
      }else if(o==5){
        this.tableHead2[0].prop = "paytype";
        this.tableHead2[0].label = "计算方式";
        this.activeIndex2 = '5';
        this.sheet = "计算方式";
      }
      this.tableData2p = this.tableDataAll2[''+this.tabHdD2S[o-1]+''];
      this.dataTotal2 = this.tableData2p.length;
      this.currentPage2 = 1;
      this.tableData2 = this.tableData2p.slice((this.currentPage2-1)*(this.pageSize2),this.currentPage2*this.pageSize2);
          
    },
    // arrTojs
    getArrJson(arr){
      var arrNew = [];
      for(var i=0;i<arr.length;i++){
        var obj ={};
        obj.id = arr[i];
        obj.agentName = arr[i];
        arrNew.push(obj);
      }
      return arrNew;
    },
    // 获取选择信息
    getData(){
      this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`,{
        params: {'ps':`uid=${this.DP.uid}`}
      }).then(function(res){
        var data =  JSON.parse(JSON.parse(res.body));
        if(data.res == "0"){
          this.agents = this.getArrJson(data.agent);
          this.platForms = this.getArrJson(data.platform);
          this.adPlaces = this.getArrJson(data.advPosition);
          this.settlWays = this.getArrJson(data.paytype);
          this.channels = this.getArrJson(data.channelName);
          this.attrs = this.getArrJson(data.affiliation);  
        }else{
          this.errorFun(data['msg'])
        }
      }).catch(function(res){
        console.log(res)
      })
    },
    findData (data){
      this.sday = formatGMTDate(new Date(new Date().getTime() - 1000*60*60*24*1),true,true);
      this.eday = formatGMTDate(new Date(new Date().getTime() - 1000*60*60*24*1),true,true);
      this.ruleForm.startDate = [this.sday,this.eday];
      this.apiAjax(this.sday,this.eday)
    },
    apiAjax(sday,eday){
      this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`,{
        params: {'ps':`sday=${sday}&eday=${eday}&agent=${this.ruleForm.agent}&platform=${this.ruleForm.platForm}&affiliation=${this.ruleForm.attr}&channelName=${this.ruleForm.channel}&paytype=${this.ruleForm.settlWay}&advPosition=${this.ruleForm.adPlace}&uid=${this.DP.uid}`}
      }).then(function(res){
        var data = JSON.parse(JSON.parse(res.body));
        console.log(data)
        if(parseInt(data['res']) === 0){
          this.loads();
          this.tableData3 = data.Totals;

          this.tableDataAll2['channelDetailDatas'] = data.channelDetailDatas;
          this.tableDataAll2['activityDetailDatas'] = data.activityDetailDatas;
          this.tableDataAll2['platformDetailDatas'] = data.platformDetailDatas;
          this.tableDataAll2['agentDetailDatas'] = data.agentDetailDatas;
          this.tableDataAll2['paytypeDetailDatas'] = data.paytypeDetailDatas;

          this.tableDataAll1['activeTranDatas'] = data.activeTranDatas;
          this.tableDataAll1['newRemainDatas'] = data.newRemainDatas;

          this.tableDataAll1['totalPayDatas'] = data.totalPayDatas;
          this.tabHdD2(1)
          this.tabHdD1(1)
        }else{
          this.errorFun(data['msg'])
        }
      }).catch(function(res){})
    },
    sortChage2({ column, prop, order }){
      this.prop = prop;
      this.order = order;
      if(this.tableData2p.slice().length>0){
        this.tableData2p = this.tableData2p.slice();
        this.tableData2p = sortBy(this.tableData2p,this.prop,this.order);
        this.currentPage2 = 1;
        this.tableData2 =this.tableData2p.slice((this.currentPage2-1)*(this.pageSize2),this.currentPage2*this.pageSize2);
        this.dataTotal2 = this.tableData2p.length;
      }
    },
    sortChage1({ column, prop, order }){
      this.prop = prop;
      this.order = order;
      if(this.tableData1p.slice().length>0){
        this.tableData1p = this.tableData1p.slice();
        this.tableData1p = sortBy(this.tableData1p,this.prop,this.order);
        this.currentPage1 = 1;
        this.tableData1 =this.tableData1p.slice((this.currentPage1-1)*(this.pageSize1),this.currentPage1*this.pageSize1);
        this.dataTotal1 = this.tableData1p.length;
      }
    },
    getSummaries(param){
     if(this.activeIndex1 == 1){
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          sums[0] = this.tableData3.dateStr;
          sums[1] = this.tableData3.channelName;
          sums[2] = this.tableData3.agent;
          sums[3] = this.tableData3.clkEquipment;
          sums[4] = this.tableData3.actEquipment;
          sums[5] = this.tableData3.newActive;
          sums[6] = this.tableData3.newEquipment;
          sums[7] = this.tableData3.newAccount;
          sums[8] = this.tableData3.newPayUser
          sums[9] = this.tableData3.newPay;
          sums[10] = this.tableData3.payrate;
          sums[11] = this.tableData3.discount;
          sums[12] = this.tableData3.cpa;
          sums[13] = this.tableData3.totalpay;
          sums[14] = this.tableData3.roi;
        });
        return sums;
     }else if(this.activeIndex1 == 2){
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          sums[index] = '';
        });
        return sums;
     }else if(this.activeIndex1 == 3){
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          sums[index] = '';
        });
        return sums;       
     }
    },
 // 表格下载
    excel(tableIndex,sheet) {
      var that = this;
      console.log(tableIndex=='2')
      if(tableIndex=='1'){
        switch(that.activeIndex1){
          case '1':
            that.sheet = '激活转化';
            break;
          case '2':
            that.sheet = '新增留存';
            break;
          case '3':
            that.sheet = '累计付费';
            break;            
        }
        //  that.xlsxTitle = "广告日数据（渠道统计）"+"-"+that.sheet+'-'+this.sday+'-'+this.eday
      }else if(tableIndex=='2'){
        switch(that.activeIndex2){
          case '1':
            that.sheet = '渠道';
            break;
          case '2':
            that.sheet = '活动';
            break;
          case '3':
            that.sheet = '平台';
            break; 
          case '4':
            that.sheet = '代理';
            break; 
          case '5':
            that.sheet = '计算方式';
            break;                                   
        }
      }
      that.xlsxTitle = "广告日数据（渠道统计）"+"-"+that.sheet+'-'+this.sday+'-'+this.eday
      that.downloadExl(that["tableHead"+tableIndex],that["tableData"+tableIndex+"p"],that.xlsxTitle,sheet);
    }
  }, 
computed:{
  // xlsx(){
  //   return this.title+'.xlsx'
  // }
},
  mounted() {
    // 初始化URL
    this.SRC = this.$store.getters.src;
    this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
    this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
    this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
    this.getData();
    this.findData(0);

  }
}